const fs = require('fs');

// createReadStream 并不是一下子就放入内存，类似于 java 中的 bufferStream
fs.createReadStream('./40_watch.js').pipe(process.stdout);

const ws = fs.createWriteStream('./test.txt');

const tid = setInterval(() => {
    const num = parseInt(Math.random() * 10);
    if(num < 7){
        ws.write(num + ''); //只能写 buffer 或字符串
    }else{
        clearInterval(tid);
        ws.end();
    }
}, 500);

ws.on('finish', () => {
    console.log("done");
});

